import { http } from '@/utils/http'

/**
 * 考试验证-进入考试前验证-步骤0
 * @param senduserId 用户ID
 * @returns 验证结果
 */
export function judgeCanExamTest(senduserId: number): Promise<IResData<void>> {
  return http.get<void>(`/api/MP/JudgeCanExamTest?senduserId=${senduserId}`)
}

/**
 * 考试信息接口
 */
export interface ExamInformation {
  examId: number
  examTitle: string
  examScore: number
  examLength: number
  passScore: number
  percentScore: number
  examShowWay: number
  examStartTime: string
  examEndTime: string
  resultFlag: number
  publishedFlag: number
  examRules: string | null
  examRuleList: any | null
  userRemainingTime: number
  allowLeavePageTimes: number
  userLeavePageTimes: number
  startCheating: number
  testTimes: number
  relationType: number
}

/**
 * 考试信息响应数据
 */
export interface ExamInformationResponse {
  info: ExamInformation
  timeDifference: number
}

/**
 * 获取考试信息- 考试信息展示页面- 步骤1
 * @param senduserId 用户ID
 * @returns 考试信息
 */
export function getExaminationInformation(
  senduserId: number,
): Promise<IResData<ExamInformationResponse>> {
  return http.get<ExamInformationResponse>(
    `/api/MP/GetExaminationInformation?senduserId=${senduserId}`,
  )
}

/**
 * 考试选项
 */
export interface QuestionOption {
  order: number
  orderShow: string
  quID: number
  answerContent: string
  answerFlag: number
  answerType: number
  qType: number //
  oldOrder: number
  optionId: number
}

/**
 * 试题
 */
export interface ExamQuestion {
  questionID: number
  qTypeShow: string
  qType: number
  questionLevel: string
  questionContent: string
  score: number
  qAnswerType: number
  questionOrder: number
  userAnswer: string[]
  userAnswerScore: number
  fillBlankCount: number
  questionAnswerKey: any
  fileUpload: any[]
  questionAnswer: QuestionOption[]
  questionAnswerTrueList: string[]
  questionAnswerTrueStr: string
  wrongQuestionFlag: number
  questionAnalysis: string
}

/**
 * 学生答案
 */
export interface StudentAnswer {
  qid: number
  qType: number
  answer: string[]
  userScore: number
}
/*
* {
    "code": 200,
    "data": {
        "exampaperID": 0,
        "exampaperTitle": null,
        "exampaperType": 0,
        "exampaperShowType": 0,
        "questionTypeStrShow": "单选题,多选题,主观题,判断题",
        "questionList": [
            {
                "questionID": 152,
                "qTypeShow": "SingleChoice",
                "qType": 2,
                "questionLevel": "中",
                "questionContent": "<p>1+1等于几？<br/></p>",
                "score": 25,
                "qAnswerType": 0,
                "questionOrder": 1,
                "userAnswer": [
                    "502"
                ],
                "userAnswerScore": 25,
                "fillBlankCount": 0,
                "questionAnswerKey": null,
                "fileUpload": [
                ],
                "questionAnswer": [
                    {
                        "order": 1,
                        "orderShow": "A",
                        "quID": 152,
                        "answerContent": "<p>1<br/></p>",
                        "answerFlag": 0,
                        "answerType": 2,
                        "qType": 2,
                        "oldOrder": 1,
                        "optionId": 501
                    },
                    {
                        "order": 2,
                        "orderShow": "B",
                        "quID": 152,
                        "answerContent": "<p>2<br/></p>",
                        "answerFlag": 1,
                        "answerType": 2,
                        "qType": 2,
                        "oldOrder": 2,
                        "optionId": 502
                    },
                    {
                        "order": 3,
                        "orderShow": "C",
                        "quID": 152,
                        "answerContent": "<p>3<br/></p>",
                        "answerFlag": 0,
                        "answerType": 2,
                        "qType": 2,
                        "oldOrder": 3,
                        "optionId": 503
                    },
                    {
                        "order": 4,
                        "orderShow": "D",
                        "quID": 152,
                        "answerContent": "<p>4<br/></p>",
                        "answerFlag": 0,
                        "answerType": 2,
                        "qType": 2,
                        "oldOrder": 4,
                        "optionId": 504
                    }
                ],
                "questionAnswerTrueList": [
                    "2"
                ],
                "questionAnswerTrueStr": "B",
                "wrongQuestionFlag": 0,
                "questionAnalysis": ""
            },
            {
                "questionID": 153,
                "qTypeShow": "MultipleChoice",
                "qType": 3,
                "questionLevel": "中",
                "questionContent": "<p>动物园里有什么？</p>",
                "score": 25,
                "qAnswerType": 0,
                "questionOrder": 2,
                "userAnswer": [
                    "505",
                    "506"
                ],
                "userAnswerScore": 25,
                "fillBlankCount": 0,
                "questionAnswerKey": null,
                "fileUpload": [
                ],
                "questionAnswer": [
                    {
                        "order": 1,
                        "orderShow": "A",
                        "quID": 153,
                        "answerContent": "<p>老虎<br/></p>",
                        "answerFlag": 1,
                        "answerType": 3,
                        "qType": 3,
                        "oldOrder": 1,
                        "optionId": 505
                    },
                    {
                        "order": 2,
                        "orderShow": "B",
                        "quID": 153,
                        "answerContent": "<p>狮子<br/></p>",
                        "answerFlag": 1,
                        "answerType": 3,
                        "qType": 3,
                        "oldOrder": 2,
                        "optionId": 506
                    },
                    {
                        "order": 3,
                        "orderShow": "C",
                        "quID": 153,
                        "answerContent": "<p>UFO<br/></p>",
                        "answerFlag": 0,
                        "answerType": 3,
                        "qType": 3,
                        "oldOrder": 3,
                        "optionId": 507
                    }
                ],
                "questionAnswerTrueList": [
                    "1",
                    "2"
                ],
                "questionAnswerTrueStr": "A、B",
                "wrongQuestionFlag": 0,
                "questionAnalysis": ""
            },
            {
                "questionID": 154,
                "qTypeShow": "EssayQuestion",
                "qType": 1,
                "questionLevel": "中",
                "questionContent": "<p>你好啊？<br/></p>",
                "score": 25,
                "qAnswerType": 0,
                "questionOrder": 3,
                "userAnswer": [
                    "11111"
                ],
                "userAnswerScore": 0,
                "fillBlankCount": 0,
                "questionAnswerKey": null,
                "fileUpload": [
                ],
                "questionAnswer": [
                ],
                "questionAnswerTrueList": [
                ],
                "questionAnswerTrueStr": "",
                "wrongQuestionFlag": 0,
                "questionAnalysis": ""
            },
            {
                "questionID": 155,
                "qTypeShow": "TrueOrFalse",
                "qType": 4,
                "questionLevel": "中",
                "questionContent": "<p>1+1=2？<br/></p>",
                "score": 25,
                "qAnswerType": 0,
                "questionOrder": 4,
                "userAnswer": [
                    "1"
                ],
                "userAnswerScore": 25,
                "fillBlankCount": 0,
                "questionAnswerKey": null,
                "fileUpload": [
                ],
                "questionAnswer": [
                ],
                "questionAnswerTrueList": [
                    "1"
                ],
                "questionAnswerTrueStr": "A、正确",
                "wrongQuestionFlag": 0,
                "questionAnalysis": ""
            }
        ],
        "studentAnswer": [
            {
                "qid": 152,
                "qType": 2,
                "answer": [
                    "502"
                ],
                "userScore": 25
            },
            {
                "qid": 153,
                "qType": 3,
                "answer": [
                    "505",
                    "506"
                ],
                "userScore": 25
            },
            {
                "qid": 154,
                "qType": 1,
                "answer": [
                    "11111"
                ],
                "userScore": 0
            },
            {
                "qid": 155,
                "qType": 4,
                "answer": [
                    "1"
                ],
                "userScore": 25
            }
        ],
        "totalScore": 100
    },
    "message": "Success",
    "serviceTime": "2025-05-01T22:49:40.6571926+08:00"
}
*/
/**
 * 试卷信息接口
 */
export interface ExampaperInformation {
  exampaperID: number
  exampaperTitle: string | null
  exampaperType: number
  exampaperShowType: number
  questionTypeStrShow: string
  questionList: ExamQuestion[]
  studentAnswer: StudentAnswer[]
  totalScore: number
}

/**
 * 根据考试人员ID，获取试卷的基本信息 -进入考试 步骤2
 * @param senduserId 用户ID
 * @returns 试卷基本信息
 */
export function getExampaperInformation(
  senduserId: number,
): Promise<IResData<ExampaperInformation>> {
  return http.get<ExampaperInformation>(`/api/MP/GetExampaperInformation?senduserId=${senduserId}`)
}

/**
 * 保存答案请求参数
 */
export interface SaveAnswerRequest {
  senduserId: number
  questionId: number
  answerData: string[] // "id1,id2"
  diff: number
}

/**
 * 保存一题的答案【答案实时保存】-考试保存答案
 * @param senduserId 用户ID
 * @param questionId 问题ID
 * @param answerData 答案数据
 * @param diff 时间差
 * @returns 保存结果
 */
export const saveUserOneAnswer = (
  senduserId: number,
  questionId: number,
  answerData: string[], // "id1,id2"
  diff: number,
): Promise<IResData<void>> => {
  const params: SaveAnswerRequest = {
    senduserId,
    questionId,
    answerData,
    diff,
  }
  return http.post<void>('/api/MP/SaveUserOneAnswer', { ...params })
}

/**
 * 提交试卷请求参数
 */
export interface SubmitAnswerRequest {
  senduserId: number
  userRemainingTime: number
  leavePageTimes: number
  timeEnd: number
  diff: number
  pmtResId: number
}

/**
 * 保存学员答案 -试卷提交
 * @param senduserId 用户ID
 * @param userRemainingTime 剩余时间
 * @param leavePageTimes 离开页面次数
 * @param timeEnd 结束时间
 * @param diff 时间差
 * @param pmtResId 资源ID
 * @returns 提交结果
 */
export const submitUserAnswer = (
  senduserId: number,
  userRemainingTime: number,
  leavePageTimes: number,
  timeEnd: number,
  diff: number,
  pmtResId: number,
): Promise<IResData<void>> => {
  return http.post<void>('/api/MP/SubmitUserAnswer', {
    senduserId,
    userRemainingTime,
    leavePageTimes: 0,
    timeEnd,
    diff,
    pmtResId,
  })
}

/**
 * 结束考试 - 中途离开考试
 * @param senduserId 用户ID
 * @returns 操作结果
 */
export const abandonExamTest = (senduserId: number): Promise<IResData<void>> => {
  return http.post<void>('/api/MP/AbandonExamTest?senduserId=' + senduserId)
}

/**
 * 考试列表项接口
 */
export interface ExamListItem {
  senduserId: number
  b_yyMMdd: string
  b_HHss: string
  e_yyMMdd: string
  e_HHss: string
  beginTime: string
  endTime: string
  examTitle: string
  testTimes: number
  allowTimes: number
  timeLength?: number
  score: number
  isfinished: boolean
  isPublishScore: number
  point: number
}

/**
 * 获取考试（全部）—左上角三个点内
 * @returns 考试列表
 */
export const getExamListForThree = (): Promise<IResData<ExamListItem[]>> => {
  return http.get<ExamListItem[]>('/api/MP/GetExamListForThree')
}

/**
 * 考试成绩详情接口
 */
export interface ExaminationResult {
  examId: number
  userId: number
  examTitle: string
  examScore: number
  examLength: number
  alreadySendScore: number
  publishedFlag: number
  passScore: number
  examTestTimes: number
  userTestTimes: number
  examStartTime: string
  examEndTime: string
  userScore: number
  isPass: number
  submitExamTime: string
  isGoON: number
  questionTypeDetail: QuestionTypeDetail[]
}

/**
 * 题型详情接口
 */
export interface QuestionTypeDetail {
  qType: string
  qTrueCount: number
  qWrongCount: number
  qNoDoneCount: number
  qAllScore: number
}

/**
 * 获取用户考试成绩详情
 * @param senduserId 用户ID
 * @returns 考试成绩详情
 */
export function getUserExaminationInformation(
  senduserId: number,
): Promise<IResData<ExaminationResult>> {
  return http.get<ExaminationResult>(
    `/api/MP/GetUserExaminationInformation?senduserId=${senduserId}`,
  )
}
